//
//  ZWLHomeViewController.swift
//  ZWLDY
//
//  Created by zwl on 2016/10/1.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit



class ZWLHomeViewController: UIViewController {
    
    // MARK:- 懒加载
    
    
    fileprivate lazy var titleView : ZWLPageTitleView = {[weak self] in
        
        let titleFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW, height: KpagetitleH)
        let titles : [String] = ["推荐","游戏","娱乐","趣玩"]
        let titleView = ZWLPageTitleView(frame: titleFrame, titles: titles)
        titleView.backgroundColor = UIColor.white
        titleView.delegate = self
        return titleView
    }()
    
    fileprivate lazy var contentView : ZWLContentView = {
        var childVcs = [UIViewController]()
        childVcs.append(ZWLRecommandViewController())
        childVcs.append(GameViewController())
        childVcs.append(ZWLYLVc())
        for _ in 0..<1 {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            childVcs.append(vc)
        }
        let contentY = kStatusBarH + kNavigationBarH + KpagetitleH
        let contentF = CGRect(x: 0, y: contentY, width: kScreenW, height: kScreenH - contentY - kTabbarH)
        let contentView = ZWLContentView(frame: contentF, childVcs: childVcs, supervc: self)
        contentView.delegate = self
        return contentView
    }()

   // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置 UI
        setupUI()
    }
}

// MARK:- titleView的代理方法
extension ZWLHomeViewController : ZWLPageTitleViewDelegate {
    func titleLabelDidClick(_ titleView: ZWLPageTitleView, selectIndex: Int) {
        contentView.setCurrentIndex(selectIndex)
    }
}

// MARK:- contentView的代理方法
extension ZWLHomeViewController : ZWLContentViewDelegate {
    func pageContentView(_ contentView: ZWLContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        titleView.setTitleWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}

// MARK:- 设置 UI 界面
extension ZWLHomeViewController {
    fileprivate func setupUI() {
        automaticallyAdjustsScrollViewInsets = false
        view.backgroundColor = UIColor.white
        // 设置导航栏
        setupNavBar()
        
       
        
        view.addSubview(titleView)
        
        view.addSubview(contentView)
 
    }
    
    func setupNavBar() {
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "logo"), for: .normal)
        leftBtn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        
        let leftBtn1 = UIButton()
        leftBtn1.setImage(UIImage(named: "image_my_history"), for: .normal)
        leftBtn1.setImage(UIImage(named: "Image_my_history_click"), for: .highlighted)
        leftBtn1.frame.size = CGSize(width: 40, height: 40)
        let rightItem1 = UIBarButtonItem(customView: leftBtn1)
        
        
        let leftBtn2 = UIButton()
        leftBtn2.setImage(UIImage(named: "btn_search"), for: .normal)
        leftBtn2.setImage(UIImage(named: "btn_search_clicked"), for: .highlighted)
        leftBtn2.frame.size = CGSize(width: 40, height: 40)
        let rightItem2 = UIBarButtonItem(customView: leftBtn2)
        
        let leftBtn3 = UIButton()
        leftBtn3.setImage(UIImage(named: "Image_scan"), for: .normal)
        leftBtn3.setImage(UIImage(named: "Image_scan_click"), for: .highlighted)
        leftBtn3.frame.size = CGSize(width: 40, height: 40)
        let rightItem3 = UIBarButtonItem(customView: leftBtn3)
        
        navigationItem.rightBarButtonItems = [rightItem1,rightItem2,rightItem3]
        
    }
}
